﻿/*
   Copyright 2012 University of Southampton

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using uk.ac.soton.ses.DAL;
using System.Web;

namespace uk.ac.soton.ses.WebControls
{
    public class ExperimentParameterLinkControl : CompositeControl
    {
        private int _parentExperimentID;
        private int _parentParameterID;
        ExperimentParameter _experimentParameter = null;
        public DropDownList ddl = null;

        public ExperimentParameterLinkControl(int parentExperimentID, int parentParameterID, ExperimentParameter experimentParameter)
        {
            this._parentExperimentID = parentExperimentID;
            this._parentParameterID = parentExperimentID;
            this._experimentParameter = experimentParameter;
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            MaterialsMetadataContext dbcontext = HttpContext.Current.GetMaterialsDBContext();

            ddl = new DropDownList();
            this.Controls.Add(ddl);
            ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
            ddl.AutoPostBack = true;
            ddl.CssClass = "ExperimentParameterLinkControl";
            ddl.Items.Add(new ListItem("[Move to parent...]", "-1"));
            ddl.Items.Add(new ListItem("[No parent]", "0"));

            int currentParameterID = this._experimentParameter.ID;

            //Find parameters for DropDownList
            IQueryable<ExperimentParameter> matchingParameters =
                dbcontext.ExperimentParameters
                .Where(param => param.Experiment.ID == this._parentExperimentID && param.ID != currentParameterID);

            //Populate DropDownList
            foreach (ExperimentParameter param in matchingParameters)
            {
                ddl.Items.Add(new ListItem(param.Name, param.ID.ToString()));
            }
        }

        void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddl == null || ddl.SelectedIndex == 0 || this._experimentParameter == null)
            {
                return;
            }

            MaterialsMetadataContext dbcontext = HttpContext.Current.GetMaterialsDBContext();

            if (ddl.SelectedIndex == 1)
            {
                dbcontext.DeleteExistingParameterParentLinks(this._experimentParameter.ID);
                dbcontext.SaveChanges();
            }
            //Create new link when selected index is a valid selection (0 is "select..." entry, 1 is "no parent")
            else
            {
                string newParentParamIDString = ddl.SelectedValue;
                int newParentParamID = -1;
                if (int.TryParse(newParentParamIDString, out newParentParamID))
                {
                    dbcontext.CreateExperimentParameterLink(newParentParamID, this._experimentParameter.ID, true);
                    dbcontext.SaveChanges();
                }
            }
            this.RaiseBubbleEvent(this, new RebindRequiredEventArgs());
        }
    }
}
